package com.esri.core.portal;

import com.esri.core.internal.io.handler.d;
import com.esri.core.internal.io.handler.h;
import com.esri.core.internal.io.handler.n;
import com.esri.core.internal.tasks.TaskListener;
import com.esri.core.internal.tasks.ags.aa;
import com.esri.core.internal.tasks.ags.af;
import com.esri.core.internal.tasks.ags.ag;
import com.esri.core.internal.tasks.ags.ai;
import com.esri.core.internal.tasks.ags.aj;
import com.esri.core.internal.tasks.ags.al;
import com.esri.core.internal.tasks.e;
import com.esri.core.internal.tasks.oauth.GetTokenTaskParameters;
import com.esri.core.internal.tasks.oauth.a;
import com.esri.core.io.EsriSecurityException;
import com.esri.core.io.UserCredentials;
import com.esri.core.map.CallbackListener;
import com.sangfor.ssl.service.utils.IGeneral;
import java.io.InputStream;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.codehaus.jackson.JsonParser;

/* loaded from: classes.dex */
public class Portal {
    public static final String DEFAULT_CULTURE;
    static final String a = "This portal has not yet connected successfully. Please wait for the PortalListener callback before using this method.";
    volatile UserCredentials b;
    private final String d;
    private volatile String e;
    private String f = null;
    private PortalUser g = null;
    private PortalInfo h = null;
    final AtomicBoolean c = new AtomicBoolean(false);

    /* loaded from: classes.dex */
    public interface GetAuthCodeCallback {
        String getAuthcode(String str);
    }

    static {
        Locale locale = Locale.getDefault();
        if (locale != null) {
            String language = locale.getLanguage();
            String country = locale.getCountry();
            if (language != null && !"".equals(language.trim()) && country != null && !"".equals(country.trim())) {
                DEFAULT_CULTURE = language + '-' + country;
                return;
            }
            DEFAULT_CULTURE = null;
            return;
        }
        DEFAULT_CULTURE = null;
    }

    public Portal(String str, UserCredentials userCredentials) {
        if (str == null || str.trim().length() == 0) {
            throw new IllegalArgumentException("Portal URL == null");
        }
        this.d = str.endsWith("/") ? str.substring(0, str.length() - 1) : str;
        a(userCredentials);
    }

    public InputStream fetchTxtFileContents() {
        try {
            return new DefaultHttpClient().execute(new HttpGet(getUrl() + "/arcgismobile.txt")).getEntity().getContent();
        } catch (Exception unused) {
            return null;
        }
    }

    public PortalQueryResultSet<PortalItem> findItems(PortalQueryParams portalQueryParams) throws Exception {
        al alVar = new al(this);
        alVar.a(portalQueryParams);
        return new aj(alVar, findSharingUrl(), this.b).execute();
    }

    public PortalQueryResultSet<PortalGroup> findGroups(PortalQueryParams portalQueryParams) throws Exception {
        al alVar = new al(this);
        alVar.a(portalQueryParams);
        return new ai(alVar, findSharingUrl(), this.b).execute();
    }

    public String getUrl() {
        return b(this.d);
    }

    public String findSharingUrl() throws EsriSecurityException, Exception {
        if (this.e == null) {
            synchronized (this.c) {
                try {
                    try {
                        fetchVersion();
                        a();
                    } catch (Exception e) {
                        throw e;
                    }
                } finally {
                }
            }
        }
        return b(this.e);
    }

    public UserCredentials getCredentials() {
        if (this.b == null) {
            return null;
        }
        return this.b.getCopy();
    }

    private void a(UserCredentials userCredentials) {
        if (userCredentials == null) {
            this.b = userCredentials;
            return;
        }
        a();
        this.b = userCredentials.getCopy();
    }

    private void a() {
        if (this.b == null || this.b.getTokenServiceUrl() != null || this.e == null) {
            return;
        }
        String replace = this.e.replace(IGeneral.PROTO_HTTP_HEAD, IGeneral.PROTO_HTTPS_HEAD);
        UserCredentials userCredentials = this.b;
        userCredentials.setTokenServiceUrl(replace + "/generateToken");
    }

    public PortalUser fetchUser() throws Exception {
        if (this.g == null && this.b != null && this.b.getUserName() != null) {
            this.g = new ag(new al(this), findSharingUrl(), this.b).execute();
        }
        return this.g;
    }

    public PortalInfo fetchPortalInfo() throws Exception {
        if (this.h == null) {
            this.h = new aa(new al(this), findSharingUrl(), this.b).execute();
        }
        return this.h;
    }

    public String fetchVersion() throws Exception {
        if (this.f == null) {
            synchronized (this.c) {
                if (this.e == null) {
                    try {
                        a(this.d + "/sharing/rest");
                        this.e = this.d + "/sharing/rest";
                        this.c.set(false);
                    } catch (Exception e) {
                        if (e instanceof EsriSecurityException) {
                            throw e;
                        }
                        a(this.d + "/sharing/");
                        this.e = this.d + "/sharing/";
                        this.c.set(true);
                    }
                } else {
                    a(this.e);
                }
            }
        }
        return this.f;
    }

    private void a(String str) throws Exception {
        this.f = new af(new al(this), str, this.b).execute();
        if (this.b == null || this.b.getAuthenticationType() != UserCredentials.AuthenticationType.NONE) {
            return;
        }
        this.b.setAuthenticationType(UserCredentials.AuthenticationType.TOKEN);
    }

    public JsonParser sendRequest(String str) throws EsriSecurityException, Exception {
        return h.a(str, (Map<String, String>) null, n.a(str, this.b));
    }

    public boolean isPreVersion162() {
        try {
            findSharingUrl();
        } catch (Exception unused) {
        }
        return this.c.get();
    }

    public JsonParser postRequest(String str) throws EsriSecurityException, Exception {
        return h.a(str, (Map<String, String>) null, true, n.a(str, this.b));
    }

    private String b(String str) {
        PortalInfo portalInfo = this.h;
        return ((portalInfo == null ? false : portalInfo.b) && str.startsWith("http:")) ? str.replace("http:", "https:") : str;
    }

    public String toString() {
        return "Portal [portalUrl=" + this.d + ", sharingUrl=" + this.e + ", version=" + this.f + ", credentials=" + this.b + ", currentuser=" + this.g + "]";
    }

    public void doOAuthUserAuthenticate(String str, String str2, GetAuthCodeCallback getAuthCodeCallback, CallbackListener<Portal> callbackListener) {
        doOAuthUserAuthenticate(str, str2, "urn:ietf:wg:oauth:2.0:oob", getAuthCodeCallback, callbackListener);
    }

    public void doOAuthUserAuthenticate(String str, String str2, String str3, GetAuthCodeCallback getAuthCodeCallback, CallbackListener<Portal> callbackListener) {
        try {
            a(str, str2, callbackListener, new GetTokenTaskParameters(getAuthCodeCallback.getAuthcode(GetTokenTaskParameters.a(findSharingUrl(), str2, str3)), str, str2, str3));
        } catch (Exception unused) {
        }
    }

    public void doOAuthAppAuthenticate(String str, String str2, CallbackListener<Portal> callbackListener) throws Exception {
        a(str, str2, callbackListener, new GetTokenTaskParameters(str, str2));
    }

    public void doOAuthDeviceAuthenticate(String str, CallbackListener<Portal> callbackListener) throws Exception {
        a(null, str, callbackListener, new GetTokenTaskParameters(str));
    }

    private void a(final String str, final String str2, final CallbackListener<Portal> callbackListener, GetTokenTaskParameters getTokenTaskParameters) throws Exception {
        final String replace = findSharingUrl().replace(IGeneral.PROTO_HTTP_HEAD, IGeneral.PROTO_HTTPS_HEAD);
        e.b.submit(new a(getTokenTaskParameters, replace, new TaskListener<com.esri.core.internal.tasks.oauth.e>() { // from class: com.esri.core.portal.Portal.1
            {
                Portal.this = this;
            }

            @Override // com.esri.core.internal.tasks.TaskListener
            public void onError(Throwable th) {
                callbackListener.onError(th);
            }

            @Override // com.esri.core.internal.tasks.TaskListener
            /* renamed from: a */
            public void onCompletion(short s, com.esri.core.internal.tasks.oauth.e eVar) {
                Portal.this.b = d.a(replace, str, str2, eVar);
                callbackListener.onCallback(Portal.this);
            }
        }));
    }
}
